<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);

$cond_vals = new stdClass(); 
$cond_vals->c = 't.status = :v1';
$cond_vals->v = array(':v1' => 1);
$opt = new stdClass();
$opt->select_expr = 'id, track_id, date, enabled_stock, enabled_delivery, enabled_member_price, brand_id, category_id, name, price, stock, stock_alert, intro, status, sold__, alert__';
$products = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'ecom_product', $cond_vals, 't.name', 'ASC', NULL, NULL, $opt)->d;

$opt = new stdClass();
$opt->select_expr = 't.*, t1.name AS _name, t1.status AS _status';
$opt->join_expr = 'LEFT JOIN ' . TABLE_NAME_PREFIX_PLUGIN . 'ecom_product AS t1 ON t.product_id = t1.id';
$recoms = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)), NULL, 't.order_number', 'ASC', NULL, NULL, $opt)->d;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.list {
  border-left: 1px solid #666;
  border-top: 1px solid #666;
  font-size: 12px;
  width: 500px;
}
table.list th.sort {
  text-align: left;
  height: 28px;
  background-color: #222;
  padding: 0 5px; 
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  font-weight: bold;
  vertical-align: middle;
  cursor: pointer;
  color: #fff;
}
table.list tr.rowodd td {
  padding: 0 5px;
  height: 25px;
  background-color: #eee;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.roweven td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
  vertical-align: middle;
}
table.list tr.rowodd:hover td {
  background-color: #f4f3b2;
}
table.list tr.roweven:hover td {
  background-color: #f4f3b2;
}
table.list tr.none td {
  padding: 0 5px;
  height: 25px;
  background-color: #fff;
  border-right: 1px solid #666;
  border-bottom: 1px solid #666;
  word-wrap: break-word;
}
.imgbtn {
  background-image: url(images/add.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
}
.imgbtn:hover {
  background-color: #eee;
}
.imgico {
  cursor: pointer;
}
select.prod {
  width: 200px;
  height: 25px;
  border: 1px solid #ddd;
}
</style>
</head>
<body>
<select class="prod" id="prod_sel">
  <?php foreach ($products as &$product) { ?>
  <option value="<?php echo $product->id; ?>"><?php echo $product->name; ?></option>
  <?php } ?>
</select>
<button class="imgbtn" onclick="add_recom();">Add</button>
<div>
  <table class="list" style="margin-top: 5px;" cellPadding="0" cellSpacing="0">
    <tr>
      <th class="sort">Product</th>
      <th class="sort" width="60px"></th>
    </tr>
    <?php foreach ($recoms as $idx => &$recom) { ?>
    <tr class="<?php echo (($idx % 2 == 0) ? 'roweven' : 'rowodd'); ?>">
      <td><?php echo $recom->_name . ($recom->_status == 0 ? ' (Invisible)' : '') ; ?></td>
      <td>
        <img <?php if ($idx == 0) echo 'style="visibility: collapse;"' ?> src="images/up.png" class="imgico" onclick="set_order('up', '<?php echo $recom->id; ?>', '<?php echo $recom->order_number; ?>')"></img>
        <img <?php if ($idx == count($recoms) - 1) echo 'style="visibility: collapse;"' ?> src="images/down.png" class="imgico" onclick="set_order('down', '<?php echo $recom->id; ?>', '<?php echo $recom->order_number; ?>')"></img>
        <img src="images/delete24.png" class="imgico" onclick="del_recom('<?php echo $recom->id; ?>');"></img>
      </td>
    </tr>
    <?php } ?>
    <?php if (count($recoms) == 0) { ?>
    <tr class="none" ><td colspan=2>No Results</td></tr>
    <?php } ?>
  </table>
</div>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript">
  function add_recom() {
    id = $('#prod_sel').find(":selected").val();
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=add_recom&q=" + encodeURIComponent(id),
      success: function(msg){
        location.reload();
      }
    });
  }

  function del_recom(id) {
    r = window.confirm('Are You Sure?');
    if (!r) {
      return;
    }
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=del_recom&q=" + encodeURIComponent(id),
      success: function(msg){
        location.reload();
      }
    });
  }

  function set_order(dir, id, num) {
    $.ajax({
      type: "POST",
      url: "<?php echo basename(__FILE__, '.php'); ?>.server.php",
      data: "action=" + dir + "&id=" + encodeURIComponent(id) + "&num=" + encodeURIComponent(num),
      success: function(msg){
        location.reload();
      }
    });
  }
</script>
</body>
</html>